<?php
//
//	file: includes/attach/class_attach_init.php
//	author: ptirhiik
//	begin: 05/03/2006
//	version: 1.6.4 - 02/12/2007
//	license: http://opensource.org/licenses/gpl-license.php GNU General Public License (GPL)
//
//	based on : attachment_mod copyright (c) 2002 Meik Sievertsen (acydburn)
//

if ( !defined('IN_PHPBB') )
{
	die('Hacking attempt');
}

// define the external version file
class attach_version
{
	function read(&$data)
	{
		$data[] = array('app_name' => 'Attachment for Categories Hierarchy', 'app_desc' => '', 'app_marker' => 'mod_attachment_CH', 'app_author' => 'Gilgraf', 'app_author_url' => 'http://ggweb-fr.com/phpbbfre/', 'app_server' => 'ptifo.clanmckeen.com', 'app_file' => '/download/versions.dta', 'app_protocol' => 'native', 'app_page' => 'http://ptifo.clanmckeen.com/download.php');
	}
}

// Get attachment mod configuration
class attach_config
{
	var $data;
	var $done;

	function attach_config()
	{
		$this->data = array();
		$this->done = false;
	}

	function read($force=false)
	{
		global $config;

		if ( !$this->done || $force )
		{
			$db_cache = new cache_config('dta_attach_config');
			$sql = 'SELECT *
						FROM ' . ATT_CONFIG_TABLE;
			$this->data = $db_cache->sql_query($sql, __LINE__, __FILE__, $force, 'config_name');
			$data['board_lang'] = trim($config->data['default_lang']);
		}
		return $this->data;
	}
}

class attach_extensions
{
	var $data;
	var $done;

	function attach_extensions()
	{
		$this->done = false;
		$this->data = array();
	}

	function read($force=false)
	{
		if ( !$this->done || $force )
		{
			$db_cache = new cache('dta_attach_group_extensions');
			$sql = 'SELECT e.extension, g.cat_id, g.download_mode, g.upload_icon, g.max_filesize, g.forum_permissions
						FROM ' . ATT_EXTENSIONS_TABLE . ' e, ' . ATT_EXT_GROUPS_TABLE . ' g
						WHERE e.ext_group_id = g.ext_group_id
							AND g.allow_group = 1';
			$this->data = $db_cache->sql_query($sql, __LINE__, __FILE__, $force, 'extension');
			$this->done = true;
		}
		return $this->data;
	}

	function encode(&$forums_list)
	{
		$res = '';
		if ( !empty($forums_list) )
		{
			$res = substr(',' . implode(',', array_map('dechex', $forums_list)) . ',', 0, 255);
			$res = substr($res, 0, strrpos(',' . $res, ','));
		}
		return $res;
	}

	function decode(&$forum_permissions)
	{
		return trim($forum_permissions) == '' ? '' : array_map('hexdec', explode(',', substr(trim($forum_permissions), 1, -1)));
	}

	function auth($forum_permissions, $forum_id, $admin_check=true)
	{
		if ( $admin_check )
		{
			global $user;
			if ( $user->data['user_level'] == ADMIN )
			{
				return true;
			}
		}
		$forums_list = array($forum_id);
		return !empty($forum_id) && ((trim($forum_permissions) == '') || strpos(' ' . $forum_permissions, $this->encode($forums_list)));
	}
}

if ( !defined('get_var') )
{
	// get_var : read $_POST/GET vars
	function get_var($var_name, $default, $multibyte = false)
	{
		global $HTTP_POST_VARS, $HTTP_GET_VARS;

		$request_var = (isset($HTTP_POST_VARS[$var_name])) ? $HTTP_POST_VARS : $HTTP_GET_VARS;
		if ( !isset($request_var[$var_name]) || (is_array($request_var[$var_name]) && !is_array($default)) || (is_array($default) && !is_array($request_var[$var_name])) )
		{
			return is_array($default) ? array() : $default;
		}

		// get type from default
		if ( !is_array($default) )
		{
			$type = gettype($default);
		}
		else
		{
			list($key_type, $type) = each($default);
			$type = gettype($type);
			$key_type = gettype($key_type);
		}

		// get value
		$var = $request_var[$var_name];
		if ( !is_array($var) )
		{
			_set_var($var, $var, $type, $multibyte);
		}
		else
		{
			$_var = $var;
			$var = array();
			foreach ($_var as $k => $v)
			{
				_set_var($k, $k, $key_type);
				if ( !is_array($v) )
				{
					_set_var($var[$k], $v, $type, $multibyte);
				}
				else
				{
					foreach ($v as $_k => $_v)
					{
						_set_var($_k, $_k, $key_type);
						_set_var($var[$k][$_k], $_v, $type, $multibyte);
					}
				}
			}
		}
		return $var;
	}

	// _set_var : type cast for get_var()
	function _set_var(&$result, $var, $type, $multibyte = false)
	{
		settype($var, $type);
		$result = $var;

		if ($type == 'string')
		{
			$result = trim(htmlspecialchars(str_replace(array("\r\n", "\r", '\xFF'), array("\n", "\n", ' '), $result)));
			// 2.0.x is doing addslashes on all variables
			$result = stripslashes($result);
			if ($multibyte)
			{
				$result = preg_replace('#&amp;(\#[0-9]+;)#', '&\1', $result);
			}
		}
	}
}

// all the stuff
if ( defined('IN_ADMIN') || in_array($requester, array('faq', INDEX, 'modcp', POSTING, 'search', 'viewtopic', 'privmsg', 'profile', 'usercp')) )
{
	$att_lang_files = array('lang_main_attach');
	if ( defined('IN_ADMIN') )
	{
		$att_lang_files[] = 'lang_admin_attach';
	}
	$user->use_lang_file($att_lang_files);
	$user->use_lang('attachcp');
	unset($att_lang_files);

	// Get Attachment Config
	$config->globals['mod_attachment_CH']['config'] = new attach_config();
	$config->globals['mod_attachment_CH']['config']->read();

	// instantiate group/extensions
	$config->globals['mod_attachment_CH']['extensions'] = new attach_extensions();

	// file handling
	$file_handler_layer = intval($config->globals['mod_attachment_CH']['config']->data['allow_ftp_upload']) ? 'includes/attach/class_file_handler_ftp' : 'includes/attach/class_file_handler_copy';
	include($config->url($file_handler_layer));
	unset($file_handler_layer);
	include($config->url('includes/attach/class_file_handler'));
}

// set our plugs
$config->set_plug_ins('mod_attachment_CH', array(
	'posting' => array(
		'layer' => array('includes/attach/class_attach_display', 'includes/attach/class_attach_parent', 'includes/attach/class_attach_' . POSTING),
		'object' => 'attach_posting',
	),
	'privmsg' => array(
		'layer' => array('includes/attach/class_attach_display', 'includes/attach/class_attach_parent', 'includes/attach/class_attach_privmsg'),
		'object' => 'attach_privmsg',
	),
	'faq' => array(
		'layer' => 'includes/attach/class_attach_faq',
		'object' => 'attach_faq',
	),
	'modcp' => array(
		'layer' => 'includes/attach/class_attach_handler',
		'object' => 'attach_handler',
	),
	'viewtopic' => array(
		'layer' => 'includes/attach/class_attach_viewtopic',
		'object' => 'attach_viewtopic',
	),
	'admin/admin_groups' => array(
		'layer' => 'includes/attach/class_attach_admin',
		'object' => 'attach_admin_groups',
	),
	'class_forums' => array(
		'layer' => 'includes/attach/class_attach_plug_ins',
		'object' => 'attach_forums',
	),
	'class_posts' => array(
		'layer' => 'includes/attach/class_attach_display',
		'object' => 'attach_display',
	),
	'class_resync.resync' => array(
		'layer' => 'includes/attach/class_attach_handler',
		'object' => 'attach_handler',
	),
	'class_resync.prune' => array(
		'layer' => 'includes/attach/class_attach_handler',
		'object' => 'attach_handler',
	),
	'class_topics_attr' => array(
		'layer' => 'includes/attach/class_attach_plug_ins',
		'object' => 'attach_topics_attr',
	),
	'class_users' => array(
		'layer' => 'includes/attach/class_attach_handler',
		'object' => 'attach_handler',
	),
	'class_versions' => array(
		'object' => 'attach_version',
	),
));

?>